package jpb.command;

import jpb.Assert;
import jpb.CommandArg;
import jpb.StartupCommandSelector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static jpb.Assert.notEmpty;
import static jpb.Assert.notNull;

/**
 * <p>
 * A {@link jpb.StartupCommandSelector} implementation that maintains an ordered list of one or more selectors and
 * returns first non-empty command. 
 *
 * @author Dariusz Kordonski
 */
public class CompositeSelector implements StartupCommandSelector {

    private final List<StartupCommandSelector> selectors;

    public CompositeSelector(List<StartupCommandSelector> selectors) {
        notEmpty("selectors", notNull("selectors", selectors));
        this.selectors = new ArrayList<StartupCommandSelector>(selectors);
    }

    public CompositeSelector(StartupCommandSelector... selectors) {
        notEmpty("selectors", notNull("selectors", selectors));
        this.selectors = new ArrayList<StartupCommandSelector>(Arrays.asList(selectors));
    }
    

    @Override
    public CommandArg command() {
        for (StartupCommandSelector selector : selectors) {
            CommandArg command = selector.command();
            if (command != null && !command.isEmpty()) {
                return command;
            }
        }
        return CommandArg.empty();
    }
    
}
